﻿using DocumentFormat.OpenXml.Drawing;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Export
{
    public class DBHelper
    {
        //private static string _connectionString = AppConfigurtaionServices.Configuration["Data:DefaultConnection:ConnectionString"];
        private static string _connectionString = "Server=db.rservice.cn;Database=dev_vehicle_1.1;Uid=dev_publish;Pwd=r@h@20200410@801;Charset=utf8;";

        public static MySqlConnection conn = new MySqlConnection(_connectionString);
        public static bool ExecuteSql(string str) {
            //var dd = cfg["conn"];
            //var connStr = "Driver={MySQL ODBC 8.0 UNICODE Driver};Server=db.rservice.cn;Port=3306;Database=dev_vehicle_1.1;User=dev_publish;Password=r@h@20200410@801;Option=3;";
            using (MySqlConnection conn = new MySqlConnection(_connectionString)) {
                using (MySqlCommand cmd = new MySqlCommand(str, conn)) {
                    try {
                        conn.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows > 0;
                    } catch (Exception ex) {
                        return false;
                    }
                }
            }
        }
        public static DataTable GetTable(string str) {
            //var dd = cfg["conn"];
            //var connStr = "Driver={MySQL ODBC 8.0 UNICODE Driver};Server=db.rservice.cn;Port=3306;Database=dev_vehicle_1.1;User=dev_publish;Password=r@h@20200410@801;Option=3;";
            using (MySqlConnection conn = new MySqlConnection(_connectionString)) {
                using (MySqlDataAdapter cmd = new MySqlDataAdapter(str, conn)) {
                    try {
                        conn.Open();
                        //MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
                        DataTable dt = new DataTable();
                        cmd.Fill(dt);
                        return dt;
                    } catch (Exception ex) {
                        return null;
                    }
                }
            }
        }
    }
}
